from django.core.cache import cache
import logging

logger = logging.getLogger(__name__)


class RedisHelper:
    """缓存操作助手"""

    @staticmethod
    def test_connection():
        """测试缓存连接"""
        try:
            test_key = "test:connection"
            test_value = "test_value"
            cache.set(test_key, test_value, timeout=10)
            result = cache.get(test_key)
            cache.delete(test_key)
            return result == test_value
        except Exception as e:
            logger.error(f"缓存连接测试失败: {str(e)}")
            return False


def test_redis_connection():
    """测试缓存连接的便捷函数"""
    return RedisHelper.test_connection()